#include "../include/pch.h"
#include <iostream>
#include "../include/connection.h"
#include "../include/commonConnectionPool.h"

using namespace std;

int main(int argc, char const *argv[])
{
    connection conn;
    conn.connect("127.0.0.1",3306,"root","123456","chat");
    /*connection conn;
    char sql[1024] = {0};
    sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
    conn.connect("127.0.0.1",3306,"root","123456","chat");
    conn.update(sql);     */
    //ConnectionPool *cp = ConnectionPool::getConnectionPool();

    clock_t begin = clock();
    // 未使用连接池
    // for (int i = 0; i < 1000; i++)
    // {
    //     connection conn;
    //     char sql[1024] = {0};
    //     sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
    //     conn.connect("127.0.0.1",3306,"root","123456","chat");
    //     conn.update(sql);
    // }
    // 使用连接池
    // ConnectionPool *cp = ConnectionPool::getConnectionPool();
    // for (int i = 0; i < 1000; i++)
    // {
    //     char sql[1024] = {0};
    //     sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
    //     shared_ptr<connection> sp = cp->getConnection();
    //     sp->update(sql);
    // }

    //// 3线程
    
    thread t1([](){
        ConnectionPool *cp = ConnectionPool::getConnectionPool();
        for (int i = 0; i < 250; i++)
        {
            // 使用连接池
            char sql[1024] = {0};
            sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            shared_ptr<connection> sp = cp->getConnection();
            sp->update(sql);
            
            // 未使用连接池
            // connection conn;
            // char sql[1024] = {0};
            // sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            // conn.connect("127.0.0.1",3306,"root","123456","chat");
            // conn.update(sql);

        }
    });
    thread t2([](){
        ConnectionPool *cp = ConnectionPool::getConnectionPool();
        for (int i = 0; i < 250; i++)
        {
            // 使用连接池
            char sql[1024] = {0};
            sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            shared_ptr<connection> sp = cp->getConnection();
            sp->update(sql);
            
            // 未使用连接池
            // connection conn;
            // char sql[1024] = {0};
            // sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            // conn.connect("127.0.0.1",3306,"root","123456","chat");
            // conn.update(sql);

        }
    });
    thread t3([](){
        ConnectionPool *cp = ConnectionPool::getConnectionPool();
        for (int i = 0; i < 250; i++)
        {
            // 使用连接池
            char sql[1024] = {0};
            sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            shared_ptr<connection> sp = cp->getConnection();
            sp->update(sql);
            
            // 未使用连接池
            // connection conn;
            // char sql[1024] = {0};
            // sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            // conn.connect("127.0.0.1",3306,"root","123456","chat");
            // conn.update(sql);
        }
    });
    thread t4([](){
        ConnectionPool *cp = ConnectionPool::getConnectionPool();
        for (int i = 0; i < 250; i++)
        {
            // 使用连接池
            char sql[1024] = {0};
            sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            shared_ptr<connection> sp = cp->getConnection();
            sp->update(sql);
            
            // 未使用连接池
            // connection conn;
            // char sql[1024] = {0};
            // sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","wyr",21,"female");
            // conn.connect("127.0.0.1",3306,"root","123456","chat");
            // conn.update(sql);
        }
    });
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    clock_t end = clock();
    cout << (end - begin) << "ms" << endl;
    return 0;
}                        
       